package com.example.demo.Controller;

import com.example.demo.Pojo.CartDTO;
import com.example.demo.Pojo.ProductDTO;
import com.example.demo.Pojo.User;
import com.example.demo.Result.Result;
import com.example.demo.Service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController("/practice")
public class AdminController {


    @Autowired
    public CartService cartServiceImpl;

    @Autowired
    public AddService addServiceImpl;


    @Autowired
    public LoginService loginServiceImpl;


    @Autowired
    public HomePageService homePageServiceImpl;

    //登录
    @PostMapping("/login")
    public Result<User> login(@RequestBody User user){

        System.out.println("login启动了");
        loginServiceImpl.login(user);
        return Result.success(user);

    }

    //购物车显示
    @GetMapping("/cartshow")
    public Result<List<CartDTO>> CartShow(){

        List<CartDTO>  cartDTO = cartServiceImpl.show();
        return Result.success(cartDTO);

    }


    //购物车商品删除界面
    @PostMapping("delete")
    public Result CartDelete(@RequestBody CartDTO cartDTO){
        cartServiceImpl.delete(cartDTO);
        return Result.success();
    }


    //主页显示界面
    @GetMapping("/homepage")
    public Result<List<ProductDTO>> HomeShow(){

        List<ProductDTO> productDTOlsit = homePageServiceImpl.ProductShow();
        return Result.success(productDTOlsit);

    }


    //支付，支付后购物车的东西会减少
    @PostMapping("payment")
    public void Payment(@RequestBody List<CartDTO> cartDTOList){
        for (CartDTO cartDTO : cartDTOList) {
            cartServiceImpl.pay(cartDTO);
        }
    }

    //加入购物车
    @PostMapping("/add")
    public Result Add(@RequestBody ProductDTO productDTO){

        addServiceImpl.add(productDTO);
        return Result.success();
    }

}
